package com.xhx.db.service.impl;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xhx.db.entity.AppVersion;
import com.xhx.db.mapper.AppVersionMapper;
import com.xhx.db.service.AppVersionService;
import com.xhx.util.PageConvertUtil;
import com.xhx.vo.PageInfoParam;
import com.xhx.vo.PageInfoResult;

import lombok.extern.slf4j.Slf4j;

/**
 * APP版本Service
 * 
 * @author xhx
 */
@Slf4j
@Service
public class AppVersionServiceImpl extends ServiceImpl<AppVersionMapper, AppVersion> implements AppVersionService {

	@Override
	public AppVersion getNewVersion(byte systemType) {
		LambdaQueryWrapper<AppVersion> query = Wrappers.lambdaQuery();
		query.eq(AppVersion::getSystemType, systemType);
		query.eq(AppVersion::getD, false);
		query.le(AppVersion::getOnlineTime, LocalDateTime.now());
		query.orderByDesc(AppVersion::getVersionNo);
		query.eq(AppVersion::getAppVersionState, 2);
		return this.getOne(query);
	}

	@Override
	public AppVersion getByNo(Integer versionNo) {
		if (versionNo == null || versionNo == 0) {
			return null;
		}
		LambdaQueryWrapper<AppVersion> query = Wrappers.lambdaQuery();
		query.eq(AppVersion::getVersionNo, versionNo);
		query.eq(AppVersion::getD, false);
		return this.getOne(query);
	}

	@Override
	public AppVersion getByName(String versionName) {
		if (ObjectUtils.isEmpty(versionName)) {
			return null;
		}
		LambdaQueryWrapper<AppVersion> query = Wrappers.lambdaQuery();
		query.eq(AppVersion::getVersionName, versionName);
		query.eq(AppVersion::getD, false);
		return this.getOne(query);
	}

	@Override
	public PageInfoResult<AppVersion> getPage(PageInfoParam<Map<String, Object>> param) {
		PageHelper.startPage(param.getPageNo(), param.getPageSize());
		List<AppVersion> list = this.getBaseMapper().getList(param.getParam());
		return PageConvertUtil.build((Page<AppVersion>) list);
	}

}
